package com.aracky.flowable.controller;

import com.aracky.common.vo.CommonResult;
import com.aracky.common.vo.PageVO;
import com.aracky.flowable.model.dto.DeploymentDTO;
import com.aracky.flowable.model.request.DeploymentRequest;
import com.aracky.flowable.model.vo.ProcessDefinitionVO;
import com.aracky.flowable.service.DeployMentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author zengzhiping
 * @BelongsPackage com.aracky.flowable.controller
 * @CreateTime 2022-08-17
 * @Description 部署流程
 */
@Api(tags = "部署流程相关接口")
@RestController
@RequestMapping("/deploy")
public class DeployMentController {

    @Resource
    private DeployMentService deployMentService;

    @ApiOperation(value = "通过文件路径进行流程部署")
    @PostMapping("savePath")
    public CommonResult<String> createDeploymentPath(@RequestBody DeploymentDTO dto) {
        String deployment = deployMentService.createDeployment(dto);
        return CommonResult.success(deployment, "流程部署成功");
    }

    @ApiOperation(value = "通过上传文件进行流程部署--暂时不采用", hidden = true)
    @PostMapping("saveFile")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile"),
            @ApiImplicitParam(name = "deployNmae", value = "部署名称", required = true, dataType = "string")
    })
    public CommonResult<String> createFileDeployMent(@RequestParam("file") MultipartFile file,
                                                     @RequestParam String deployNmae) throws IOException {
        String deployment = deployMentService.createByteDeployMent(file, deployNmae);
        return CommonResult.success(deployment, "流程部署成功");
    }


    @ApiOperation("获取流程部署分页列表")
    @PostMapping("pageList")
    public CommonResult<PageVO<ProcessDefinitionVO>> getProcessDefinitionList(@RequestBody DeploymentRequest deploymentRequest) {
        return CommonResult.success(deployMentService.getProcessDefinitionList(deploymentRequest));
    }


    @ApiOperation("查询部署流程图")
    @GetMapping("showImage")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "deploymentId", value = "流程部署id", required = true, dataType = "string"),
            @ApiImplicitParam(name = "diagramResourceName", value = "流程图片名称", required = true, dataType = "string")
    })
    public ResponseEntity<byte[]> getDeptloyImage(String deploymentId, String diagramResourceName) {
        return deployMentService.getDeptloyImage(deploymentId, diagramResourceName);
    }


}
